package com.ylsk.mobilecheck.service.impl;

import com.google.api.client.util.Lists;
import com.ylsk.check.domain.ReportApprLog;
import com.ylsk.check.domain.dto.CheckRecordDTO;
import com.ylsk.check.domain.dto.RectificationReviewDTO;
import com.ylsk.check.domain.emum.BasicInfoTemplateEnum;
import com.ylsk.check.domain.emum.ConstructionStatusEnum;
import com.ylsk.check.domain.emum.ReportStatusEnum;
import com.ylsk.check.domain.emum.SearchTypeEnum;
import com.ylsk.check.domain.so.CheckRecordSO;
import com.ylsk.check.domain.so.ReportInfoSO;
import com.ylsk.check.domain.vo.*;
import com.ylsk.check.service.*;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.mobilecheck.mapper.ApparitorMobileMapper;
import com.ylsk.mobilecheck.service.ApparitorMobileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ApparitorMobileServiceImpl implements ApparitorMobileService {

    @Autowired
    private IReportApprLogService reportApprLogService;

    @Autowired
    private ICheckItemService checkItemService;

    @Autowired
    private ICheckRecordService checkRecordService;

    @Autowired
    private IRectificationReviewService rectificationReviewService;

    @Autowired
    private ApparitorMobileMapper apparitorMobileMapper;

    @Autowired
    private ICheckProblemService checkProblemService;

    @Autowired
    private IReportInfoService reportInfoService;
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<ReportInfoVO> listReportInfo(ReportInfoSO reportInfoSO) {
        String searchType = reportInfoSO.getSearchType();
        if(StringUtils.isEmpty(searchType)){
            return Lists.newArrayList();
        }

        if(searchType.equals(SearchTypeEnum.AUDIT.getCode())){
            reportInfoSO.setOperateUserId(SecurityUtils.getUserId());
            return apparitorMobileMapper.selectAuditReportInfoList(reportInfoSO);
        }else{
            if(searchType.equals(SearchTypeEnum.REPORT_RECORD.getCode())){
                reportInfoSO.setConstructionStatus(ReportStatusEnum.PASS.getCode());
            }
            List<ReportInfoVO> reportInfoVOList = apparitorMobileMapper.selectReportInfoList(reportInfoSO);
            for (ReportInfoVO reportInfoVO : reportInfoVOList) {
                String basicInfo = reportInfoVO.getBasicInfo();
                String tbName = reportInfoVO.getTbName();
                Map<String, Object> objectMap = BasicInfoTemplateEnum.getBasicInfo(tbName, basicInfo.split(","));
                reportInfoVO.setBasicInfoMap(objectMap);
            }
            return reportInfoVOList;
        }
    }

    @Override
    @DataScope(deptAlias = "te",userAlias = "tf")
    public List<ReportInfoVO> selectListReportInfo(ReportInfoSO reportInfoSO) {
        return reportInfoService.selectreportInfoList(reportInfoSO);
    }

    @Override
    public int apprReportInfo(ReportApprLog reportApprLog) {
        return reportApprLogService.insertReportApprLog(reportApprLog);
    }

    @Override
    public List<CheckItemVO> selectCheckItemList() {
        return checkItemService.selectCheckItemList(null);
    }

    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<?> listCheckRecord(CheckRecordSO checkRecordSO) {
        checkRecordSO.setOperateUserId(SecurityUtils.getUserId());
        String searchType = checkRecordSO.getSearchType();

        if(StringUtils.isEmpty(searchType)){
            return Lists.newArrayList();
        }
        if(searchType.equals("currentProject")){
            return apparitorMobileMapper.selectCheckProjectList(checkRecordSO);
        }else{
            return apparitorMobileMapper.selectCheckRecordList(checkRecordSO);
        }
    }

    @Override
    public int saveCheckRecord(CheckRecordDTO checkRecordDTO) {
        return checkRecordService.insertCheckRecord(checkRecordDTO);
    }

    @Override
    public CheckRecordDetailVO getCheckRecord(Long checkRecordId) {
        return checkRecordService.getCheckRecrdDetailInfo(checkRecordId);
    }

    @Override
    public CheckProblemVO getCheckProblem(Long checkProblemId) {
        return checkProblemService.selectCheckProblemByCheckProblemId(checkProblemId);
    }

    @Override
    public List<TodayCheckTaskVO> selectTodayCheckTaskList(CheckRecordSO checkRecordSO) {
        return checkRecordService.selectTodayCheckTaskList(checkRecordSO);
    }

    @Override
    public int saveRectificationReview(RectificationReviewDTO rectificationReviewDTO) {
        return rectificationReviewService.insertRectificationReview(rectificationReviewDTO);
    }

    @Override
    public List<ReportInfoVO> listProjectEnd(ReportInfoSO reportInfoSO) {
        reportInfoSO.setConstructionStatus(ConstructionStatusEnum.END.getCode());
        return reportInfoService.selectreportInfoList(reportInfoSO);
    }
}
